import { getToken, setToken, removeToken } from "@/utils/auth";
import { login, logout, getInfo } from "@/api/login";
import { defineStore } from "pinia";

const useUserStore = defineStore("user", {
  state: () => ({
    token: getToken(),
    id: "",
    name: "",
    nickName: "",
    avatar: "",
    roles: [],
    permissions: [],
  }),
  actions: {
    // 登录
    login(loginForm) {
      // 接收整个表单对象
      const { username, password, code, uuid } = loginForm; // 解构
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then((res) => {
            setToken(res.token);
            this.token = res.token;
            resolve(res);
          })
          .catch((err) => {
            console.error("login api error:", err); // 一定要打印
            reject(err);
          });
      });
    },
  },
});

export default useUserStore;
